/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dto.Order;
import dto.OrderStatus;
import dto.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import util.IdbUtil;

/**
 *
 * @author duonghung1369
 */
public class OrderDAO extends BaseDAO<Order> {

    Log log = LogFactory.getLog(OrderDAO.class);

    public OrderDAO(IdbUtil _database) {
        super(_database);
    }

    @Override
    public Collection<Order> getAll() throws SQLException {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        try {
            List<Order> orders = new ArrayList<Order>();
            String sql = "select * from "
                    + "([order] join [user] on [order].customer_id = [user].id) "
                    + "join order_status on [order].order_status_id = order_status.id";
            con = _database.getConnection();
            stm = con.prepareStatement(sql);
            rs = stm.executeQuery();

            Order tmpOrder = null;
            User user = null;
            OrderStatus orderStatus = null;
            while (rs.next()) {
                tmpOrder = new Order();
                user = new User();
                orderStatus = new OrderStatus();

                user.setId(rs.getInt(11));
                user.setUsername(rs.getString(12));
                user.setPassword(rs.getString(13));

                orderStatus.setId(rs.getInt(18));
                orderStatus.setDescription(rs.getString(19));

                tmpOrder.setId(rs.getInt("id"));
                tmpOrder.setPlaced_date(rs.getDate("placed_datetime"));
                tmpOrder.setDelivered_date(rs.getDate("delivered_datetime"));
                tmpOrder.setUpdate_date(rs.getDate("update_date"));
                tmpOrder.setShipping_charges(rs.getFloat("shipping_charges"));
                tmpOrder.setIs_deleted(rs.getBoolean("is_deleted"));
                tmpOrder.setConfirmation_string(rs.getString("confirmation_string"));
                tmpOrder.setNote(rs.getString("note"));
                tmpOrder.setUser(user);
                tmpOrder.setOrder_status(orderStatus);

                orders.add(tmpOrder);
            }


            return orders;
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stm != null) {
                    stm.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (SQLException ex) {
                log.error("OrderDAO.getAll()", ex);
            }
        }
    }

    @Override
    public Order get(int id) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Order create(Order entity) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void update(Order entity) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean delete(int id) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
